﻿// Copyright (c) zhenlei520 All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace EInfrastructure.Core.EFCore.MySql
{
    /// <summary>
    ///
    /// </summary>
    public class BaseDbContext : DbContext
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <param name="type">Map实现的接口</param>
        protected virtual void OnModelCreating<TDbContext>(ModelBuilder modelBuilder, Type type)
            where TDbContext : DbContext
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.AutoMap<TDbContext>(type);
        }

        #region 提交保存

        /// <summary>
        /// 提交保存
        /// </summary>
        /// <returns></returns>
        public virtual bool Commit()
        {
            this.SaveChanges();
            return true;
        }

        #endregion

        #region 异步提交保存

        /// <summary>
        /// 异步提交保存
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public virtual async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = new CancellationToken())
        {
            await base.SaveChangesAsync(cancellationToken);
            return true;
        }

        #endregion
    }
}
